// Reverse.cpp: implementation of the Reverse class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Expression.h"
#include "Reverse.h"
#include "Simple_Expression.h"
#include "var.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Reverse::Reverse()
{

}

Reverse::~Reverse()
{

}

BOOL Reverse::Interpret()
{
	CString strTemp;
	strTemp = AAnalyzer()->GetCurSymbol();

	ComplierNote* pNote = NULL;
	
	if(strTemp == _T("("))
	{
		pNote = new Simple_Expression;
		pNote->SetOwner(this);
		LeftNote(pNote);
		AAnalyzer()->MoveNextSymbol();
		if(!pNote->Interpret())
		{
			this->ErrorInfo(pNote->ErrorInfo());
			AAnalyzer()->MovePrevSymbol();
			return FALSE;
		}
		strTemp = AAnalyzer()->GetCurSymbol();
		if(strTemp == _T(")"))
		{
			AAnalyzer()->MoveNextSymbol();
			return TRUE;
		}
		return FALSE;
	}
	else
	{
		pNote = new Var;
		pNote->SetOwner(this);
		LeftNote(pNote);
		if(!pNote->Interpret())
		{
			this->ErrorInfo(pNote->ErrorInfo());
			return FALSE;
		}
		return TRUE;
	}
}

Variable Reverse::Execute()
{
	Variable pVar = LeftNote()->Execute();
	
	return -pVar;
}